﻿<%@ WebHandler Language="C#" Class="DownHandler" %>

using System.IO;
using System.Text;
using System.Web;
using System.Web.SessionState;

public class DownHandler : IHttpHandler, IRequiresSessionState
{
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            //string id = context.Request.QueryString["id"] as string;
            var downloadurl = context.Request.QueryString["downloadurl"];
            if (string.IsNullOrEmpty(downloadurl) || !File.Exists(context.Server.MapPath(downloadurl)) || Path.GetExtension(context.Server.MapPath(downloadurl)).ToUpper() != ".DOC")
            {
                //context.Session["errorInfo"] = "要下载的文件不存在!";
                // context.Response.Redirect("~/Error.aspx", true);
                //context.Response.Write("<script>alert('111');</script>");
                //context.Response.Flush();
                //context.Response.End();
                // return;
                context.Response.Write("<script>alert('非法操作');history.go(-1);</script>");
            }
            else
            {
                #region download

                var destFileName = context.Server.MapPath(downloadurl);
                var fi = new FileInfo(destFileName);
                context.Response.Clear();
                context.Response.ClearHeaders();
                context.Response.Buffer = true;
                context.Response.Charset = "GBK";
                var browser = context.Request.UserAgent.ToUpper();

                if (browser.Contains("MS") && browser.Contains("IE"))
                {
                    context.Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                                         + HttpUtility.UrlEncode(Path.GetFileName(destFileName), Encoding.UTF8));
                }
                else if (browser.Contains("FIREFOX"))
                {
                    var outputFileName = "\"" + Path.GetFileName(destFileName) + "\"";
                    context.Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                                         + outputFileName);
                }
                else
                {
                    context.Response.AppendHeader("Content-Disposition", "attachment;filename="
                                                                         + HttpUtility.UrlEncode(Path.GetFileName(destFileName), Encoding.UTF8));
                }

                //添加头信息，为 "文件下载/另存为 "对话框指定默认文件名  

                context.Response.AppendHeader("Content-Length", fi.Length.ToString());
                context.Response.ContentType = "application/octet-stream";
                context.Response.Filter.Close();
                context.Response.WriteFile(destFileName);
                context.Response.Flush();
                context.Response.End();

                #endregion
            }
        }
        catch
        {
            context.Session["errorInfo"] = "要下载的文件不存在,造成文件下载失败!";
            context.Response.Redirect("~/Error.aspx", true);
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}